﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BitTorrentSharp.Peers
{
	/// <summary>
	/// Represent as a message that request a data piece from another peer.
	/// </summary>
	public class RequestMessage : PeerNonKeepAliveMessage
	{
		/// <summary>
		/// Get or set the index of the piece in peice set.
		/// </summary>
		public int Index
		{
			get;
			set;
		}

		/// <summary>
		/// Get or set the begin position of the data in bytes.
		/// </summary>
		public int Begin
		{
			get;
			set;
		}

		/// <summary>
		/// Get or set the length of the data in bytes.
		/// </summary>
		public int Length
		{
			get;
			set;
		}

		/// <summary>
		/// Get the type of the message. This property always returns <see cref="PeerMessageType.Request"/>.
		/// </summary>
		public override PeerMessageType Type
		{
			get
			{
				return PeerMessageType.Request;
			}
		}

		/// <summary>
		/// Get the data sequence of this message.
		/// </summary>
		/// <returns>The data sequence of this message.</returns>
		protected override byte[] GetPayloadBytes()
		{
			return ByteHelper.Concat(BitConverter.GetBytes(Index), BitConverter.GetBytes(Begin), BitConverter.GetBytes(Length)).EnsureBigEndian();
		}

		/// <summary>
		/// Initialize a new instance of the message with the specified arguments.
		/// </summary>
		/// <param name="index">The index of the piece in peice set.</param>
		/// <param name="begin">The begin position of the data in bytes.</param>
		/// <param name="length">The length of the data in bytes.</param>
		public RequestMessage(int index, int begin, int length)
		{
			this.Index = index;
			this.Begin = begin;
			this.Length = length;
		}

		/// <summary>
		/// Initialize a new instance of the message with the specified arguments with all zero propeties.
		/// </summary>
		public RequestMessage()
			: this(0, 0, 0)
		{
		}
	}
}
